﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using umbraco.MacroEngines;
using umbraco.MacroEngines.Library;

namespace uComponents.RazorModels.MultipleDates
{
	[RazorDataTypeModel("F88229E1-7928-4BED-AA8D-8FC2CA7FDCB5")]
	public class MultipleDatesModelBinder : IRazorDataTypeModel
	{
		public bool Init(int CurrentNodeId, string PropertyData, out object instance)
		{
			var dates = new List<DateTime>();

			if (!string.IsNullOrEmpty(PropertyData))
			{
				var values = PropertyData.Split(',').Select(s => s.Trim()).ToList();
				foreach (var value in values)
				{
					DateTime date;
					if (DateTime.TryParse(value, out date))
					{
						dates.Add(date);
					}
				}
			}

			instance = dates;

			return true;
		}
	}
}
